/**
 * @author LKQ
 * @date 2021/11/30 10:23
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {0,1,2,2,3,0,4,2};
        solution.removeElement(nums, 2);
    }

    public int removeElement(int[] nums, int val) {
        int n = nums.length, fast = 0, slow = 0;
        if (n==1 && val!=nums[n-1]) {
            return 1;
        }
        while (fast < n) {
            if (nums[slow]!=val) {
                fast = slow;
                ++slow;
                ++fast;
            }else {
                if (nums[fast]!=val) {
                    // 找到不等的，交换，交换后slow+1, fast+1
                    int temp = nums[slow];
                    nums[slow] = nums[fast];
                    nums[fast] = temp;
                    slow++;
                }
                fast++;
            }
        }
        return slow;
    }
}
